﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Text;
using System.Drawing;

namespace HZ.Supervise.Web.Secondphase.Comparison
{
    public partial class ComparisonByAreaSettlement : BasePage
    {

        DataTable dt = new DataTable();
        DataTable dt1 = new DataTable();
        DataTable dt2 = new DataTable();
        Dictionary<string, string> dicHosName = new Dictionary<string, string>();
        SeriesChartType t = SeriesChartType.Line;
        string[] aid = { };
        protected void Page_Load(object sender, EventArgs e)
        {
            txtAreaName.Attributes.Add("readonly", "readonly");
            //dataBind();
            //BindChart();
        }

        /// <summary>
        /// 图表绑定 
        /// </summary>
        private void BindChart()
        {
            this.Page.RegisterStartupScript("", "<script>dis('chart3');</script>");
            #region 三个金额
            //空为初始页
            if (dt.Rows.Count > 0)
            {
                string areaid = "";//次数
                //string sort = "0";//属性 0:基药 1:非基药
                //IList<string> list = new List<string>();
                //Dictionary<string, Series> dicSsumAmount = new Dictionary<string, Series>();
                //Dictionary<string, Series> dicSamount = new Dictionary<string, Series>();
                //Dictionary<string, Series> dicSubsidyAmount = new Dictionary<string, Series>();
                Dictionary<string, Dictionary<string, double>> dicSsumAmount = new Dictionary<string, Dictionary<string, double>>();
                Dictionary<string, Dictionary<string, double>> dicSamount = new Dictionary<string, Dictionary<string, double>>();
                Dictionary<string, Dictionary<string, double>> dicSubsidyAmount = new Dictionary<string, Dictionary<string, double>>();
                for (int i = 0; i < aid.Length - 1; i++)
                {
                    dicSsumAmount.Add(aid[i], new Dictionary<string, double>());
                    dicSamount.Add(aid[i], new Dictionary<string, double>());
                    dicSubsidyAmount.Add(aid[i], new Dictionary<string, double>());
                }
                #region 绑定值
                #region 绑定值(3个金额)
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (!areaid.Equals(dt.Rows[i]["PAreaId"].ToString()))//医院一样
                    {
                        areaid = dt.Rows[i]["PAreaId"].ToString();
                    }
                    dicSsumAmount[areaid].Add(dt.Rows[i]["Time"].ToString(), double.Parse(dt.Rows[i]["PtotalAmount"].ToString()));
                    dicSamount[areaid].Add(dt.Rows[i]["Time"].ToString(), double.Parse(dt.Rows[i]["PEssentialDrugsAmount"].ToString()));
                    dicSubsidyAmount[areaid].Add(dt.Rows[i]["Time"].ToString(), double.Parse(dt.Rows[i]["PSbsidyAmount"].ToString()));
                }
                List<string> listmonth = GetPageMonthall(DateTime.Parse(txtStratTime.Text.Trim()), DateTime.Parse(txtEndTime.Text.Trim()));//得到选择时间段内多少个月
                //给没有值的月补值
                for (int i = 0; i < aid.Length - 1; i++)
                {
                    foreach (var item in listmonth)
                    {
                        if (!dicSsumAmount[aid[i]].ContainsKey(item))
                        {
                            dicSsumAmount[aid[i]].Add(item, 0);
                        }
                        if (!dicSamount[aid[i]].ContainsKey(item))
                        {
                            dicSamount[aid[i]].Add(item, 0);
                        }
                        if (!dicSubsidyAmount[aid[i]].ContainsKey(item))
                        {
                            dicSubsidyAmount[aid[i]].Add(item, 0);
                        }
                    }
                }


                #endregion
                #endregion

                #region 数据绑定到图
                //左图
                for (int i = 0; i < aid.Length - 1; i++)
                {
                    Series sess = new Series();
                    sess.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Spline", true);
                    sess.LegendText = dicHosName[aid[i]].ToString();
                    sess.IsValueShownAsLabel = true;
                    sess["DrawingStyle"] = "Emboss";
                    Series sess2 = new Series();
                    sess2.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Spline", true);
                    sess2.LegendText = dicHosName[aid[i]].ToString();
                    sess2.IsValueShownAsLabel = true;
                    sess2["DrawingStyle"] = "Emboss";
                    Series sess3 = new Series();
                    sess3.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Spline", true);
                    sess3.LegendText = dicHosName[aid[i]].ToString();
                    sess3.IsValueShownAsLabel = true;
                    sess3["DrawingStyle"] = "Emboss";
                    for (int j = 0; j < listmonth.Count; j++)
                    {
                         sess.Points.AddXY(listmonth[j].Substring(4) + "月", (dicSsumAmount[aid[i]][listmonth[j]]/10000).ToString("0.0000"));
                        sess2.Points.AddXY(listmonth[j].Substring(4) + "月", (dicSamount[aid[i]][listmonth[j]]/10000).ToString("0.0000"));
                        sess3.Points.AddXY(listmonth[j].Substring(4) + "月", (dicSubsidyAmount[aid[i]][listmonth[j]]/10000).ToString("0.0000"));
                        sess.Points[j].ToolTip = (dicSsumAmount[aid[i]][listmonth[j]]/10000).ToString("0.0000");
                        sess2.Points[j].ToolTip = (dicSamount[aid[i]][listmonth[j]] / 10000).ToString("0.0000");
                        sess3.Points[j].ToolTip = (dicSubsidyAmount[aid[i]][listmonth[j]] / 10000).ToString("0.0000");
                    }
                     
                    hospitalAmountSubsidy.Series.Add(sess);
                    hospitalAmountSubsidy0.Series.Add(sess2);
                    hospitalAmountSubsidy1.Series.Add(sess3);
                }
                #endregion

                #region 两张图片属性

                hospitalAmountSubsidy.ChartAreas["hospitalAmountSubsidy"].AxisX.IsMarginVisible = true;
                hospitalAmountSubsidy.ChartAreas["hospitalAmountSubsidy"].Area3DStyle.Enable3D = false;
                hospitalAmountSubsidy.Titles.Add("处方单总金额对比(万元)");
                hospitalAmountSubsidy.Titles[0].Font = new Font("宋体", 16, FontStyle.Bold);
                hospitalAmountSubsidy.Titles[0].Position.X = 30;
                hospitalAmountSubsidy.Titles[0].Position.Y = 1;
                hospitalAmountSubsidy.Titles[0].Position.Width = 50;
                hospitalAmountSubsidy.Titles[0].Position.Height = 8;
                hospitalAmountSubsidy.ChartAreas["hospitalAmountSubsidy"].AxisX.Interval = 1;
                hospitalAmountSubsidy.ChartAreas["hospitalAmountSubsidy"].AxisX.IntervalOffset = 1;
                hospitalAmountSubsidy.ChartAreas["hospitalAmountSubsidy"].AxisX.LabelStyle.IsStaggered = true;





                hospitalAmountSubsidy0.ChartAreas["hospitalAmountSubsidy0"].AxisX.IsMarginVisible = true;
                hospitalAmountSubsidy0.ChartAreas["hospitalAmountSubsidy0"].Area3DStyle.Enable3D = false;
                hospitalAmountSubsidy0.Titles.Add("基药总金额对比(万元)");
                hospitalAmountSubsidy0.Titles[0].Font = new Font("宋体", 16, FontStyle.Bold);
                hospitalAmountSubsidy0.Titles[0].Position.X = 30;
                hospitalAmountSubsidy0.Titles[0].Position.Y = 1;
                hospitalAmountSubsidy0.Titles[0].Position.Width = 50;
                hospitalAmountSubsidy0.Titles[0].Position.Height = 8;
                hospitalAmountSubsidy0.ChartAreas["hospitalAmountSubsidy0"].AxisX.Interval = 1;
                hospitalAmountSubsidy0.ChartAreas["hospitalAmountSubsidy0"].AxisX.IntervalOffset = 1;
                hospitalAmountSubsidy0.ChartAreas["hospitalAmountSubsidy0"].AxisX.LabelStyle.IsStaggered = true;

                hospitalAmountSubsidy1.ChartAreas["hospitalAmountSubsidy1"].AxisX.IsMarginVisible = true;
                hospitalAmountSubsidy1.ChartAreas["hospitalAmountSubsidy1"].Area3DStyle.Enable3D = false;
                hospitalAmountSubsidy1.Titles.Add("补贴总金额对比(万元)");
                hospitalAmountSubsidy1.Titles[0].Font = new Font("宋体", 16, FontStyle.Bold);
                hospitalAmountSubsidy1.Titles[0].Position.X = 30;
                hospitalAmountSubsidy1.Titles[0].Position.Y = 1;
                hospitalAmountSubsidy1.Titles[0].Position.Width = 50;
                hospitalAmountSubsidy1.Titles[0].Position.Height = 8;
                hospitalAmountSubsidy1.ChartAreas["hospitalAmountSubsidy1"].AxisX.Interval = 1;
                hospitalAmountSubsidy1.ChartAreas["hospitalAmountSubsidy1"].AxisX.IntervalOffset = 1;
                hospitalAmountSubsidy1.ChartAreas["hospitalAmountSubsidy1"].AxisX.LabelStyle.IsStaggered = true;

                hospitalAmountSubsidy.ChartAreas["hospitalAmountSubsidy"].AxisY.Title = "金额(万元)";
                hospitalAmountSubsidy.ChartAreas["hospitalAmountSubsidy"].AxisX.Title = "月份";

                hospitalAmountSubsidy0.ChartAreas["hospitalAmountSubsidy0"].AxisY.Title = "金额(万元)";
                hospitalAmountSubsidy0.ChartAreas["hospitalAmountSubsidy0"].AxisX.Title = "月份";

                hospitalAmountSubsidy1.ChartAreas["hospitalAmountSubsidy1"].AxisY.Title = "金额(万元)";
                hospitalAmountSubsidy1.ChartAreas["hospitalAmountSubsidy1"].AxisX.Title = "月份";
                #endregion

                this.lblnull.Visible = false;
            }
            else
            {
                this.hospitalAmountSubsidy.Visible = false;
                this.hospitalAmountSubsidy0.Visible = false;
                lblnull.Visible = true;
            }
            #endregion
            #region 2个比例
            if (dt1.Rows.Count > 0)
            {
                string hosId = "";
                string month = "";
                Dictionary<string, Dictionary<string, double>> dicPamount = new Dictionary<string, Dictionary<string, double>>();
                Dictionary<string, Dictionary<string, double>> dicPcount = new Dictionary<string, Dictionary<string, double>>();
                for (int i = 0; i < aid.Length - 1; i++)
                {
                    dicPamount.Add(aid[i], new Dictionary<string, double>());
                    dicPcount.Add(aid[i], new Dictionary<string, double>());
                }
                //金额的3个变量
                double temp0 = 0;
                double sum = 0;
                double ratio = 0;
                //数量的3个变量
                double tempcount = 0;
                double sumcount = 0;
                double ratiocount = 0;


                int count = 0;
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    if (month.Equals(dt1.Rows[i]["Time"].ToString()))
                    {
                        if (hosId == dt1.Rows[i]["PAreaId"].ToString())
                        {
                            //医院一样 
                            if (dt1.Rows[i]["PsortSource"].ToString().Equals("0"))
                            {
                                temp0 = double.Parse(dt1.Rows[i]["Pamount"].ToString());
                                sum += double.Parse(dt1.Rows[i]["Pamount"].ToString());
                                tempcount = double.Parse(dt1.Rows[i]["Pcount"].ToString());
                                sumcount += double.Parse(dt1.Rows[i]["Pcount"].ToString());
                            }
                            else
                            {
                                sum += double.Parse(dt1.Rows[i]["Pamount"].ToString());
                                sumcount += double.Parse(dt1.Rows[i]["Pcount"].ToString());

                            }
                        }
                        else
                        {
                            //得到上个医院的使用比例
                            ratio = temp0 / sum;
                            dicPamount[hosId].Add(month, ratio);
                            ratiocount = tempcount / sumcount;
                            dicPcount[hosId].Add(month, ratiocount);
                            hosId = dt1.Rows[i]["PAreaId"].ToString();
                            temp0 = 0;
                            sum = 0;
                            tempcount = 0;
                            sumcount = 0;
                            //给下一个医院赋值
                            if (dt1.Rows[i]["PsortSource"].ToString().Equals("0"))
                            {
                                temp0 = double.Parse(dt1.Rows[i]["Pamount"].ToString());
                                sum += double.Parse(dt1.Rows[i]["Pamount"].ToString());
                                tempcount = double.Parse(dt1.Rows[i]["Pcount"].ToString());
                                sumcount += double.Parse(dt1.Rows[i]["Pcount"].ToString());
                            }
                            else
                            {
                                sum += double.Parse(dt1.Rows[i]["Pamount"].ToString());
                                sumcount += double.Parse(dt1.Rows[i]["Pcount"].ToString());
                            }
                        }

                    }
                    else
                    {

                        //时间不一样的处理
                        if (i != 0)
                        {
                            ratio = temp0 / sum;
                            dicPamount[hosId].Add(month, ratio);
                            ratiocount = tempcount / sumcount;
                            dicPcount[hosId].Add(month, ratiocount);
                        }
                        hosId = dt1.Rows[i]["PAreaId"].ToString();
                        month = dt1.Rows[i]["Time"].ToString();

                        temp0 = 0;
                        sum = 0;
                        tempcount = 0;
                        sumcount = 0;
                        //给下一个医院赋值
                        if (dt1.Rows[i]["PsortSource"].ToString().Equals("0"))
                        {
                            temp0 = double.Parse(dt1.Rows[i]["Pamount"].ToString());
                            sum += double.Parse(dt1.Rows[i]["Pamount"].ToString());
                            tempcount = double.Parse(dt1.Rows[i]["Pcount"].ToString());
                            sumcount += double.Parse(dt1.Rows[i]["Pcount"].ToString());
                        }
                        else
                        {
                            sum += double.Parse(dt1.Rows[i]["Pamount"].ToString());
                            sumcount += double.Parse(dt1.Rows[i]["Pcount"].ToString());
                        }
                    }

                }
                List<string> listmonth = GetPageMonthall(DateTime.Parse(txtStratTime.Text.Trim()), DateTime.Parse(txtEndTime.Text.Trim()));
                //给没有值的月补值
                for (int i = 0; i < aid.Length - 1; i++)
                {
                    foreach (var item in listmonth)
                    {
                        if (!dicPamount[aid[i]].ContainsKey(item))
                        {
                            dicPamount[aid[i]].Add(item, 0);
                        }
                        if (!dicPcount[aid[i]].ContainsKey(item))
                        {
                            dicPcount[aid[i]].Add(item, 0);
                        }
                    }
                }
                // Dictionary<string, Series> dichos = new Dictionary<string, Series>();

                for (int i = 0; i < aid.Length - 1; i++)
                {
                    Series sess = new Series();
                    sess.ChartType = SeriesChartType.Spline;
                    sess.LegendText = dicHosName[aid[i]].ToString();
                    sess.IsValueShownAsLabel = true;
                    sess["DrawingStyle"] = "Emboss";
                    Series sess2 = new Series();
                    sess2.ChartType = SeriesChartType.Spline;
                    sess2.LegendText = dicHosName[aid[i]].ToString();
                    sess2.IsValueShownAsLabel = true;
                    sess2["DrawingStyle"] = "Emboss";
                    foreach (var item in listmonth)
                    {
                        sess.Points.AddXY(item.Substring(4) + "月", (dicPamount[aid[i]][item] * 100).ToString("0.00"));
                        sess2.Points.AddXY(item.Substring(4) + "月", (dicPcount[aid[i]][item] * 100).ToString("0.00"));
                    }
                    hospitalAmountSubsidy2.Series.Add(sess);
                    hospitalAmountSubsidy3.Series.Add(sess2);
                }

                hospitalAmountSubsidy2.ChartAreas["hospitalAmountSubsidy2"].AxisX.IsMarginVisible = true;
                hospitalAmountSubsidy2.ChartAreas["hospitalAmountSubsidy2"].Area3DStyle.Enable3D = false;
                hospitalAmountSubsidy2.Titles.Add("基药使用率(金额)对比(%)");
                hospitalAmountSubsidy2.Titles[0].Font = new Font("宋体", 16, FontStyle.Bold);
                hospitalAmountSubsidy2.Titles[0].Position.X = 30;
                hospitalAmountSubsidy2.Titles[0].Position.Y = 1;
                hospitalAmountSubsidy2.Titles[0].Position.Width = 50;
                hospitalAmountSubsidy2.Titles[0].Position.Height = 8;
                hospitalAmountSubsidy2.ChartAreas["hospitalAmountSubsidy2"].AxisY.Maximum = 100;
                hospitalAmountSubsidy2.ChartAreas["hospitalAmountSubsidy2"].AxisX.Interval = 1;
                hospitalAmountSubsidy2.ChartAreas["hospitalAmountSubsidy2"].AxisX.IntervalOffset = 1;
                hospitalAmountSubsidy2.ChartAreas["hospitalAmountSubsidy2"].AxisX.LabelStyle.IsStaggered = true;
                hospitalAmountSubsidy2.ChartAreas["hospitalAmountSubsidy2"].AxisY.Title = "比例(%)";
                hospitalAmountSubsidy2.ChartAreas["hospitalAmountSubsidy2"].AxisX.Title = "月份";

                hospitalAmountSubsidy3.ChartAreas["hospitalAmountSubsidy3"].AxisX.IsMarginVisible = true;
                hospitalAmountSubsidy3.ChartAreas["hospitalAmountSubsidy3"].Area3DStyle.Enable3D = false;
                hospitalAmountSubsidy3.Titles.Add("基药使用率(数量)对比(%)");
                hospitalAmountSubsidy3.Titles[0].Font = new Font("宋体", 16, FontStyle.Bold);
                hospitalAmountSubsidy3.Titles[0].Position.X = 30;
                hospitalAmountSubsidy3.Titles[0].Position.Y = 1;
                hospitalAmountSubsidy3.Titles[0].Position.Width = 50;
                hospitalAmountSubsidy3.Titles[0].Position.Height = 8;
                hospitalAmountSubsidy3.ChartAreas["hospitalAmountSubsidy3"].AxisY.Maximum = 100;
                hospitalAmountSubsidy3.ChartAreas["hospitalAmountSubsidy3"].AxisX.Interval = 1;
                hospitalAmountSubsidy3.ChartAreas["hospitalAmountSubsidy3"].AxisX.IntervalOffset = 1;
                hospitalAmountSubsidy3.ChartAreas["hospitalAmountSubsidy3"].AxisX.LabelStyle.IsStaggered = true;
                hospitalAmountSubsidy3.ChartAreas["hospitalAmountSubsidy3"].AxisY.Title = "比例(%)";
                hospitalAmountSubsidy3.ChartAreas["hospitalAmountSubsidy3"].AxisX.Title = "月份";
            }
            else
            {
                this.hospitalAmountSubsidy1.Visible = false;
                this.hospitalAmountSubsidy2.Visible = false;
                lblnull.Visible = true;
            }
            #endregion

        }
        /// <summary>
        /// 得到时间段内一共有多少个月返回月份
        /// </summary>
        /// <param name="timefrom"></param>
        /// <param name="timeto"></param>
        /// <returns></returns>
        private List<string> GetPageMonthall(DateTime timefrom, DateTime timeto)
        {
            string year = timeto.Year.ToString(); //截至年份
            int nmonth = timeto.Month;//截至月份
            TimeSpan objT = timeto.Subtract(timefrom);//获得时间内一共多少时间
            int month2 = 0;
            //if (objT.Days %((365 / 12))>0)//判断间隔是否大于1年
            //{
            if (timeto.Day == 1)
            {
                if (timefrom.Day == 1)
                {
                    month2 = System.Convert.ToInt32(objT.Days / ((365 / 12)));
                }
                else
                {
                    month2 = System.Convert.ToInt32(objT.Days / ((365 / 12))) + 1;
                }

            }
            else
            {
                if (objT.Days % ((365 / 12)) > timeto.Day)
                {
                    month2 = System.Convert.ToInt32(objT.Days / ((365 / 12))) + 2;
                }
                else
                {
                    month2 = System.Convert.ToInt32(objT.Days / ((365 / 12))) + 1;
                }

            }
            int nmm = 0;
            List<string> dd = new List<string>();
            for (int mm = nmonth; (mm > 0 && nmm < month2); mm--)
            {
                if (timeto.Day == 1)
                {
                    dd.Add(year + AddZeromonth(mm - 1));
                    if (mm - 1 == 1)
                    {
                        nmm++;
                        break;
                    }
                }
                else
                {
                    dd.Add(year + AddZeromonth(mm));
                }

                nmm++;
            }
            month2 = month2 - nmm;
            nmm = 0;
            int yuarTemp = 1;
            while (nmm < month2)
            {
                if (nmm < 12)
                {
                    dd.Add((Convert.ToInt32(year) - yuarTemp).ToString() + AddZeromonth(12 - nmm));
                    nmm++;
                }
                else
                {
                    month2 = month2 - 12;
                    nmm = 0;
                    yuarTemp++;
                    dd.Add((Convert.ToInt32(year) - yuarTemp).ToString() + AddZeromonth(12 - nmm));
                    nmm++;
                }
            }
            dd.Sort();
            return dd;
        }
        /// <summary>
        /// 给月份补0
        /// </summary>
        /// <param name="month"></param>
        /// <returns></returns>
        public string AddZeromonth(int month)
        {
            if (month.ToString().Length < 2)
                return "0" + month.ToString();
            else
                return month.ToString();
        }
        /// <summary>
        /// 数据绑定
        /// </summary>
        public void dataBind()
        {
            StringBuilder strid = new StringBuilder();
            StringBuilder where = new StringBuilder();
            if (!string.IsNullOrEmpty(HidAreaId.Value.Trim()))
            {
                aid = HidAreaId.Value.Trim().Split(',');
            }
            else
            {
                this.Page.RegisterStartupScript("", "<script>alert('请选择对比的地区')</script>");
                return;
            }
            if (txtStratTime.Text.Trim().Length < 1 && txtEndTime.Text.Trim().Length < 1)
            {
                this.Page.RegisterStartupScript("", "<script>alert('请选择对比时间')</script>");
                return;
            }
            foreach (string item in aid)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    if (item.Equals("530101"))
                    {
                        strid.Append("'530100',");
                        continue;
                    }
                    strid.Append("'" + item + "',");
                }
            }
            if (!string.IsNullOrEmpty(txtStratTime.Text.Trim()))
            {
                where.Append(" and PSubmitTime> '" + txtStratTime.Text.Trim() + "'");
            }
            if (!string.IsNullOrEmpty(txtEndTime.Text.Trim()))
            {
                where.Append(" and PSubmitTime<'" + txtEndTime.Text.Trim() + "'");
            }

            string sql = "select dbo.fun_GetYearMonth(PSubmitTime) as Time,sum(PtotalAmount) as PtotalAmount,SUM(PEssentialDrugsAmount) as PEssentialDrugsAmount,SUM(PSbsidyAmount) as PSbsidyAmount,PAreaId,MIN(PAreaname) as PAreaname from TB_Prescription where POrderStatus>0 and  PAreaId in (" + strid.Remove(strid.Length - 1, 1).ToString() + ") " + where + " group by dbo.fun_GetYearMonth(PSubmitTime),PAreaId";
            string sql2 = "select SUM(Pamount) as Pamount, sum(Pcount) as Pcount  ,dbo.fun_GetYearMonth(PSubmitTime) as time,PsortSource,PAreaId,MAX(PAreaname) as  PAreaname from View_Prescription where PrescriptionID in( select PrescriptionId from TB_Prescription where POrderStatus>0 and  PAreaId in (" + strid.ToString() + ") " + where + ") group by dbo.fun_GetYearMonth(PSubmitTime),PAreaId,PsortSource";
            string sql3 = "select * from TB_Area where AreaID in (" + strid + ")";
            HZ.Supervise.IBLL.IBLL_Prescription iPrescription = HZ.Supervise.BLLFactory.IBl_Prescription();
            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
            DataTable dtHosName = iPrescription.GetSubsidyStatistics(sql3);
            dt = iPrescription.GetSubsidyStatistics(sql);
            dt1 = iPrescription.GetSubsidyStatistics(sql2);
            dicHosName.Clear();
            for (int i = 0; i < dtHosName.Rows.Count; i++)
            {
                dicHosName.Add(dtHosName.Rows[i]["AreaID"].ToString(), dtHosName.Rows[i]["AreaName"].ToString());
            }
        }

        /// <summary>
        /// 排名的数据绑定
        /// </summary>
        public void dataBindPM()
        {
            StringBuilder strid = new StringBuilder();
            StringBuilder where = new StringBuilder();
            if (!string.IsNullOrEmpty(HidAreaId.Value.Trim()))
            {
                aid = HidAreaId.Value.Trim().Split(',');
            }
            else
            {
                this.Page.RegisterStartupScript("", "<script>alert('请选择对比的地区')</script>");
                return;
            }
            if (txtStratTime.Text.Trim().Length < 1 && txtEndTime.Text.Trim().Length < 1)
            {
                this.Page.RegisterStartupScript("", "<script>alert('请选择对比时间')</script>");
                return;
            }

            foreach (string item in aid)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    if (item.Equals("530101"))
                    {
                        strid.Append("'530100',");
                        continue;
                    }
                    strid.Append("'" + item + "',");
                }
            }
            if (!string.IsNullOrEmpty(txtProcureCatalogID.Text))
            {
                where.Append(" and ProductName like '%" + txtProcureCatalogID.Text + "%'");
            }
            if (!string.IsNullOrEmpty(txtStratTime.Text.Trim()))
            {
                where.Append(" and PSubmitTime> '" + txtStratTime.Text.Trim() + "'");
            }
            if (!string.IsNullOrEmpty(txtEndTime.Text.Trim()))
            {
                where.Append(" and PSubmitTime<'" + txtEndTime.Text.Trim() + "'");
            }
            string sql = "select SUM(Pamount) as Pamount,SUM(Pcount) as Pcount,dbo.fun_GetYearMonth(PSubmitTime) as Time,PAreaId ,MAX(PAreaname) as PAreaname from view_Prescription  where POrderStatus>0 and   PAreaId in (" + strid.Remove(strid.Length - 1, 1).ToString() + ") " + where + " group by dbo.fun_GetYearMonth(PSubmitTime),PAreaId order by Time";

            HZ.Supervise.IBLL.IBLL_Prescription iPrescription = HZ.Supervise.BLLFactory.IBl_Prescription();
            dt2 = iPrescription.GetSubsidyStatistics(sql);
            string areaId = "";
            string month = "";
            double Pamount = 0;
            double Pcount = 0;
            if (dt2.Rows.Count > 0)
            {
                Dictionary<string, Dictionary<string, double>> dicPamount = new Dictionary<string, Dictionary<string, double>>();
                Dictionary<string, Dictionary<string, double>> dicPcount = new Dictionary<string, Dictionary<string, double>>();
                for (int i = 0; i < aid.Length - 1; i++)
                {
                    dicPamount.Add(aid[i], new Dictionary<string, double>());
                    dicPcount.Add(aid[i], new Dictionary<string, double>());
                }
                int count = 0;
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    if (areaId == dt2.Rows[i]["PAreaId"].ToString())
                    {
                        month = dt2.Rows[i]["Time"].ToString();
                        Pamount = double.Parse(dt2.Rows[i]["Pamount"].ToString());
                        Pcount = double.Parse(dt2.Rows[i]["Pcount"].ToString());
                        dicPamount[areaId].Add(month, Pamount);
                        dicPcount[areaId].Add(month, Pcount);
                    }
                    else
                    {
                        month = dt2.Rows[i]["Time"].ToString();
                        areaId = dt2.Rows[i]["PAreaId"].ToString();
                        Pamount = double.Parse(dt2.Rows[i]["Pamount"].ToString());
                        Pcount = double.Parse(dt2.Rows[i]["Pcount"].ToString());
                        dicPamount[areaId].Add(month, Pamount);
                        dicPcount[areaId].Add(month, Pcount);
                    }

                }
                List<string> listmonth = GetPageMonthall(DateTime.Parse(txtStratTime.Text.Trim()), DateTime.Parse(txtEndTime.Text.Trim()));
                //给没有值的月补值
                for (int i = 0; i < aid.Length - 1; i++)
                {
                    foreach (var item in listmonth)
                    {
                        if (!dicPamount[aid[i]].ContainsKey(item))
                        {
                            dicPamount[aid[i]].Add(item, 0);
                        }
                        if (!dicPcount[aid[i]].ContainsKey(item))
                        {
                            dicPcount[aid[i]].Add(item, 0);
                        }
                    }
                }
                for (int i = 0; i < aid.Length - 1; i++)
                {
                    Series sess = new Series();
                    sess.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Spline", true);
                    sess.LegendText = dicHosName[aid[i]].ToString();
                    sess.IsValueShownAsLabel = true;
                    sess["DrawingStyle"] = "Emboss";
                    Series sess2 = new Series();
                    sess2.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Spline", true);
                    sess2.LegendText = dicHosName[aid[i]].ToString();
                    sess2.IsValueShownAsLabel = true;
                    sess2["DrawingStyle"] = "Emboss";
                    foreach (var item in listmonth)
                    {
                        sess.Points.AddXY(item.Substring(4) + "月", (dicPamount[aid[i]][item] / 10000).ToString("0.000"));
                        sess2.Points.AddXY(item.Substring(4) + "月", (dicPcount[aid[i]][item] / 10000).ToString("0.000"));
                    }
                    hospitalAmountSubsidy4.Series.Add(sess);
                    hospitalAmountSubsidy5.Series.Add(sess2);
                }

                hospitalAmountSubsidy4.ChartAreas["hospitalAmountSubsidy4"].AxisX.IsMarginVisible = true;
                hospitalAmountSubsidy4.ChartAreas["hospitalAmountSubsidy4"].Area3DStyle.Enable3D = false;
                hospitalAmountSubsidy4.Titles.Add("药品用量(金额)对比图");
                hospitalAmountSubsidy4.Titles[0].Font = new Font("微软雅黑", 16, FontStyle.Bold);
                hospitalAmountSubsidy4.Titles[0].Position.X = 30;
                hospitalAmountSubsidy4.Titles[0].Position.Y = 1;
                hospitalAmountSubsidy4.Titles[0].Position.Width = 50;
                hospitalAmountSubsidy4.Titles[0].Position.Height = 8;
                hospitalAmountSubsidy4.ChartAreas["hospitalAmountSubsidy4"].AxisX.Interval = 1;
                hospitalAmountSubsidy4.ChartAreas["hospitalAmountSubsidy4"].AxisX.IntervalOffset = 1;
                hospitalAmountSubsidy4.ChartAreas["hospitalAmountSubsidy4"].AxisX.LabelStyle.IsStaggered = true;
                hospitalAmountSubsidy4.ChartAreas["hospitalAmountSubsidy4"].AxisY.Title = "金额(万元)";
                hospitalAmountSubsidy4.ChartAreas["hospitalAmountSubsidy4"].AxisX.Title = "月份";


                hospitalAmountSubsidy5.ChartAreas["hospitalAmountSubsidy5"].AxisX.IsMarginVisible = true;
                hospitalAmountSubsidy5.ChartAreas["hospitalAmountSubsidy5"].Area3DStyle.Enable3D = false;
                hospitalAmountSubsidy5.Titles.Add("药品用量(数量)对比图");
                hospitalAmountSubsidy5.Titles[0].Font = new Font("微软雅黑", 16, FontStyle.Bold);
                hospitalAmountSubsidy5.Titles[0].Position.X = 30;
                hospitalAmountSubsidy5.Titles[0].Position.Y = 1;
                hospitalAmountSubsidy5.Titles[0].Position.Width = 50;
                hospitalAmountSubsidy5.Titles[0].Position.Height = 8;
                hospitalAmountSubsidy5.ChartAreas["hospitalAmountSubsidy5"].AxisX.Interval = 1;
                hospitalAmountSubsidy5.ChartAreas["hospitalAmountSubsidy5"].AxisX.IntervalOffset = 1;
                hospitalAmountSubsidy5.ChartAreas["hospitalAmountSubsidy5"].AxisX.LabelStyle.IsStaggered = true;

                hospitalAmountSubsidy5.ChartAreas["hospitalAmountSubsidy5"].AxisY.Title = "金额(万元)";
                hospitalAmountSubsidy5.ChartAreas["hospitalAmountSubsidy5"].AxisX.Title = "月份";
            }
            else
            {
                this.hospitalAmountSubsidy4.Visible = false;
                this.hospitalAmountSubsidy5.Visible = false;
                lblnull.Enabled = true;
            }

        }
        protected void btnPm_Click(object sender, EventArgs e)
        {
            dataBindPM();
            this.Page.RegisterStartupScript("", "<script>dis('chart6')</script>");
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            dataBind();
            BindChart();
        }
    }
}
